package com.networklib.http.impl.post;

import com.networklib.http.exception.NetException;
import com.networklib.http.ResponseResult;
import com.networklib.http.impl.BaseHttpRequestImpl;
import com.networklib.http.other.HttpMethod;

import java.io.IOException;
import java.util.UUID;

import okhttp3.Call;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by ASUS on 2017/6/13.
 */

public abstract class PostHttpRequestImpl extends BaseHttpRequestImpl {

    public PostHttpRequestImpl(String url) throws NetException {
        super(url);
    }

    @Override
    public HttpMethod getMathd() {
        return HttpMethod.POST;
    }

    protected abstract RequestBody getParams();

    @Override
    public final ResponseResult execute() throws IOException {
        Request request = new Request.Builder()
                .url(url)
                .post(getParams())
                .build();
        Call call = client.newCall(request);
        Response response = call.execute();
        ResponseResult responseResult = new ResponseResult(UUID.randomUUID().toString(), call);
        responseResult.setResponse(response);
        return responseResult;
    }
}
